package com.example.servlet.bookServlet;

import com.example.dao.BookDao;

import com.example.entity.Book;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/bookgetid")
public class BookGetByIdServlet extends HttpServlet {
    
    private BookDao bookDao = new BookDao();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        String idStr = request.getParameter("id");
        try {
            int id = Integer.parseInt(idStr);
            Book book = bookDao.getBookById(id);

            if (book  != null) {
                request.setAttribute("book", book);
                request.getRequestDispatcher("updatebook.jsp").forward(request, response);
            } else {
                response.sendRedirect("index.jsp");
            }
        } catch (NumberFormatException e) {
            response.sendRedirect("index.jsp");
        }

    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
//        request.setCharacterEncoding("UTF-8");
//
//            // 获取表单数据
//            Student student = new Student();
//            student.setId(Integer.parseInt(request.getParameter("id")));
//            student.setStudentId(request.getParameter("studentId"));
//            student.setName(request.getParameter("name"));
//            student.setGrade(request.getParameter("grade"));
//            student.setClasses(request.getParameter("classes"));
//            student.setEmail(request.getParameter("email"));
//        System.out.println("更新用户id为："+request.getParameter("id"));
//        //int i = Integer.parseInt(request.getParameter("id"));
//
//        // 更新学生信息
//        boolean success = false;
//        try {
//            success = studentDao.update(student);
//            if (success) {
//                response.sendRedirect("studentList");
//            } else {
//                request.setAttribute("error", "更新失败");
//                request.setAttribute("student", student);
//                request.getRequestDispatcher("updatestudent.jsp").forward(request, response);
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//


    }
}